/**
 * 渠道管理管理初始化
 */
var ChannelInfo = {
    id: "ChannelInfoTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1,
    isAdmin: false
};

/**
 * 初始化表格的列
 */
ChannelInfo.initColumn = function () {
    return [[
        {title: '应用名称', field: 'appName', align: 'center',width: 200},
        {title: '渠道名称', field: 'channelName',align: 'center',width: 150},
        {title: '推广链接', field: 'promoteUrl', align: 'center'},
        {title: '创建时间', field: 'createTime', align: 'center',width:150, templet: function (res) {
                if (!$.trim(res.createTime)) {
                    return "";
                } else {
                    return new Date(res.createTime).format("yyyy-MM-dd");
                }
            }},
        {title: '操作', field: 'id', align: 'center',width: 100, templet:function (res) {
                return "<div style='display: flex;flex-direction: row;align-items: center'>" +
                    "<div style='color: #bd0404;margin: auto;' onclick=\"ChannelInfo.delete('"+res.channelId+"')\">删除</div>" +
                    "</div>";
            }}
    ]];
};


ChannelInfo.adminColumn = function () {
    return [[
        {title: '商户名称', field: 'merchantName',align: 'center',width: 100},
        {title: '渠道名称', field: 'channelName',align: 'center',width: 150},
        {title: '应用名称', field: 'appName', align: 'center',width: 200},
        {title: '推广链接', field: 'promoteUrl', align: 'center'},
        {title: '创建时间', field: 'createTime', align: 'center',width:150, templet: function (res) {
                if (!$.trim(res.createTime)) {
                    return "";
                } else {
                    return new Date(res.createTime).format("yyyy-MM-dd");
                }
            }},
        {title: '操作', field: 'id', align: 'center',width: 100, templet:function (res) {
                return "<div style='display: flex;flex-direction: row;align-items: center'>" +
                    "<div style='color: #bd0404;margin: auto;' onclick=\"ChannelInfo.delete('"+res.channelId+"')\">删除</div>" +
                    "</div>";
            }}
    ]];
};

/**
 * 点击添加渠道管理
 */
ChannelInfo.openAddChannelInfo = function () {
    var index = layer.open({
        type: 2,
        title: '添加渠道管理',
        area: ['800px', '420px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/channelInfo/channelInfo_add'
    });
    this.layerIndex = index;
};

/**
 * 删除渠道管理
 */
ChannelInfo.delete = function (channelId) {
    layer.confirm("注意：此项删除，会将此项渠道统一删除？",
        {btn:['确认','取消']},
        function () {
            layer.closeAll()
            layer.load(0,{
                shade:false
            });

            var ajax = new $ax(Feng.ctxPath + "/channelInfo/delete", function (data) {
                Feng.success("删除成功!");
                ChannelInfo.refresh();
                layer.closeAll();
            }, function (data) {
                layer.closeAll();
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("channelId", channelId);
            ajax.start();
        },function () {
            layer.msg("操作已取消",{icon:1})
        });
};
/**
 * 刷新
 */
ChannelInfo.refresh = function(){
    ChannelInfo.table.reload("tableReload",{
        page: {
            curr: 1 //重新从第1页开始
        },
    });
}

ChannelInfo.tableList = function (queryData) {
    layui.use("table", function () {
        var table = layui.table;
        ChannelInfo.table = table;
        table.render({
            elem: "#table",
            url: Feng.ctxPath + "/channelInfo/list",
            cellMinWidth: 120,
            where: queryData,
            page: true,
            limit: 20,
            cols: ChannelInfo.isAdmin?ChannelInfo.adminColumn():ChannelInfo.initColumn(),
            parseData: function (res) {
                console.log(res);
                return {
                    "code": 0,
                    "msg": "获取成功",
                    "count": res.total,
                    "data": res.items
                }
            },
            text: {
                none: '暂无相关数据'
            },
            id: "tableReload"
        });
    });
};

$(function () {
    if ($("#isAdmin").val() == "超级管理员"){
        ChannelInfo.isAdmin = true;
    }else{
        ChannelInfo.isAdmin = false;
    }
    DateUtils.expandDate();
    ChannelInfo.tableList();
});
